package com.whmnx.chitchat.ui.chat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.utils.TUIKitConstants;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.LoginBean;
import com.whmnx.chitchat.bean.UserBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.main.LoginActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.CommonDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author : huang
 * date   : 2020/8/13 0013 16:26
 * version: 1.0
 *
 * @author Administrator
 */
public class ChatSettingActivity extends BaseActivity {
    @BindView(R.id.cv_user)
    CircleImageView cvUser;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tv_del)
    TextView tvDel;

    private ChatInfo chatInfo;
    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.chat_seting_activity);
        setTitle("聊天设置");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.NICKPOP)) {
                tvName.setText(messageEvent.getMessageValue());
            }
        }
    }

    @Override
    protected void loadData() {
        String id = getIntent().getStringExtra("id");
        if (!TextUtils.isEmpty(id)){
            Map<String, Object> params = new HashMap<>();
            params.put("userId", id);
            HttpClient.get(ChatSettingActivity.this, Constants.GETUSERINFO, params, new CallBack<UserBean>() {
                @Override
                public void onSuccess(UserBean loginBean) {  //13797054533
                    tvName.setText(loginBean.getUserNick());
                    ImageUtil.setImage(cvUser, loginBean.getHeadImg());
                   // setTitle(loginBean.getUserNick());
                    chatInfo = new ChatInfo();
                    chatInfo.setId(loginBean.getUserId());
                }
            });
        }else {
            chatInfo = (ChatInfo) getIntent().getSerializableExtra("chatInfo");
            ImageUtil.setImage(cvUser, SPUtil.getString(Constants.FORMPHOTO));
            tvName.setText(chatInfo.getChatName());
        }
    }




    @OnClick({R.id.tv_search, R.id.tv_del,R.id.cv_user,R.id.add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_search:
                Intent intent = new Intent(UItils.getContext(), SearchActivity.class);
                //  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(TUIKitConstants.GroupType.GROUP, false);
                intent.putExtra("toUserId", chatInfo.getId());
                intent.putExtra("isTopChat", chatInfo.isTopChat());
                startActivity(intent);
                break;
            case R.id.cv_user:
                startActivity(new Intent(this,ChatPerSettingActivity.class).putExtra("ToUserID",chatInfo.getId()));
                break;
            case R.id.add:
                startActivity(new Intent(this,AddGroupActivity.class).putExtra("type",5));
                break;
            case R.id.tv_del:
                String s2 = "确定清空记录吗?";
                new CommonDialog(ChatSettingActivity.this, R.style.dialog, s2, (dialog, confirm) -> {
                    if (confirm) {
                        delAllMsg();
                    }
                    dialog.dismiss();
                }).show();

                break;
            default:
        }
    }

    private void delAllMsg() {
        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("ToUserID",chatInfo.getId());
        if (chatInfo.isTopChat()){
            params.put("RecordType","1");
        }else {
            params.put("RecordType","0");
        }

        HttpClient.get(this, Constants.GETDELETECHATRECORD, params,new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {
                UItils.showToastSafe("清空记录成功");
                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setMessageKey(EventConstants.DELMSG);
                EventBus.getDefault().post(messageEvent);
            }
        });
    }
}
